package org.demo.bar;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.demo.sorting.Node;

/**
 * @author dzhang
 *
 */
public class DataLoader {
	public final Logger logger = Logger.getLogger(getClass());
	
	private static DataLoader _dataLoader;
	private Node _head;
	
	private String _file="seed.txt";
	
	private DataLoader() {
		String delimiter = ",";
		try {
			String fileName = Thread.currentThread().getContextClassLoader()
					.getResource(_file).getFile();
			if (logger.isDebugEnabled()) {
				logger.debug("file is " + fileName);
			}
			BufferedReader bufferedReader = new BufferedReader(new FileReader(
					fileName));
			String line = bufferedReader.readLine();
			_head = new Node(0);
			Node t = _head;
			if( line!= null) {
				String[] items = line.split(delimiter);
				//convert list of string to Nodes
				for(String n : items) {
					t.next = new Node(Integer.parseInt(n));
					t = t.next;
				}
			}
//			while ((line = bufferedReader.readLine()) != null) {
//				String[] items = line.split(delimiter);				
//			}// end while
			

		} catch (FileNotFoundException fe) {
			fe.printStackTrace();
		} catch (IOException ie) {
			ie.printStackTrace();
		}
	}
	
	
	public Node getHead() {
		return _head;
	}

	public static synchronized DataLoader getInstance() {
		if(_dataLoader == null) {
			_dataLoader = new DataLoader();			
		}
		return _dataLoader;
	}
}
